
void init()
{
    if (!session) 
    {
      session = request.getSession(true);
    }
}


void log(String msg)
{
    session.getServletContext().log(msg)
}


String openPreTag()
{
    def str = "<div style=\" border: 2px dotted #0a0; padding: 1ex;\"><pre style=\"width:99%; overflow:auto; border:1px solid #aaa; padding:0; margin:0\">"
    return str
}


String closePreTag()
{
    return "</pre></div>"
}

String getPreBody()
{
    return """
    <html>
      <head>
        <title>Timeline View</title>
        <link rel="stylesheet" type="text/css" href="styles2.css">

      <head>
    	<body onresize="onResize();">

    		<div id="content">
    """
}


String getPostBody()
{
    return """
    </div>
    </body>
    """
}


void myresponse()
{
    def domainName = request.getParameter("domainName") // MyDomain
    def domainDocsLocation = request.getParameter("domainDocsLocation") // http://localhost:8080/soatimeline/MyDomain/

    log("BEGIN: Domain Processing Servlet with Domain Name:"+domainName+" Domain Docs Location:"+domainDocsLocation)

    if(domainName == null)
    {
        domainName = "MyDomain"
        domainDocsLocation = new File( ConfigVars.DOMAIN_GEN_HOME + "/" + domainName ).getCanonicalPath()
    }

    println getPreBody()
    println "<h1>Domain Views Generation</h1>"
    println "<h3>You entered Domain name as: <a href='" + domainName + "'>" + domainName + "</a></h3>"
    println "<h3>You entered Domain Source Docs location as: <a href='" + domainDocsLocation + "'>" + domainDocsLocation + "</a></h3>" 
    println "<h3>Looking for inputs:</h3>"
    println "<h5>Domain List file: <a href='" + domainDocsLocation+ "/MyDomain_list.csv" + "'>" + domainDocsLocation + "/MyDomain_list.csv" + "</a></h3>" 
    println "<h5>Domain Graph file: <a href='" + domainDocsLocation+ "/MyDomain_graph.csv" + "'>" + domainDocsLocation + "/MyDomain_graph.csv" + "</a></h3>" 
    println "<h5>and Domain Timeline file: <a href='" + domainDocsLocation+ "/MyDomain_timeline.csv" + "'>" + domainDocsLocation + "/MyDomain_timeline.csv" + "</a></h3>" 
    println "<h3>Generating the views for your domain: <a href='" + domainName + "'>" + domainName + "</a></h3>"
    //println "<img id='loading' src='loading.gif'></a>"

    def domainGenHome = ConfigVars.DOMAIN_GEN_HOME
    def groovyHome =  ConfigVars.GROOVY_HOME
    def domainDocs = "${domainGenHome}/"

    //def cmdLine = "echo ${domainGenHome} ${groovyHome}"
    def groovyLauncher = new File("${groovyHome}/bin/groovy").getCanonicalPath()
    def cparg = "-cp"
    def cp= []
    cp.add( new File("${domainGenHome}/src/model").getCanonicalPath() )
    cp.add( new File("${domainGenHome}/src/impl").getCanonicalPath())
    cp.add( new File("${domainGenHome}/lib/ostermillerutils_1_07_00.jar").getCanonicalPath( ))
    cp.add( new File("${domainGenHome}/lib/joda-time-1.5.jar").getCanonicalPath() )
    cp.add( new File("${domainGenHome}/lib/oy-lm-1.4.jar").getCanonicalPath() )
    
    // now collect the classpath
    def classpath = cp[0]
    cp.each { str -> classpath = classpath+":"+str}
    
    // remove the last : in the classpath
    //classpath.setCharAt(str.lastIndexOf(':'), ' ' as char)

    def groovyProgram = new File("${domainGenHome}/src/impl/NodeDomainTransforms.groovy").getCanonicalPath()
    def arguments = "${domainName} ${domainDocsLocation}"
    //str = "${groovyHome}/bin/groovy -cp ${domainGenHome}/src/model:${domainGenHome}/src/impl:${domainGenHome}/lib/ostermillerutils_1_07_00.jar:${domainGenHome}/lib/joda-time-1.5.jar:${domainGenHome}//lib/oy-lm-1.4.jar ${domainGenHome}/src/impl/NodeDomainTransforms.groovy ${domainName} ${domainDocsLocation}"
    def cmdLine = groovyLauncher + " " + cparg + " " + classpath + " " + groovyProgram + " " + arguments
    println "<h3>Executing:</h3>"+openPreTag()+cmdLine+closePreTag()

    log("Domain name:"+domainName)
    log("Domain Gen Home:"+domainGenHome)
    log("Domain Docs Location:"+domainDocsLocation)
    log("using classpath:"+cp)
    log("Executing command line:"+cmdLine)
    
    def p = cmdLine.execute()
    //p.consumeProcessOutput()
    p.waitForOrKill(20000)
    //p.waitFor()

    log("Finished Executing command line")

    //println "<script>document.getElementById('loading').style.visibility='hidden'</script>"
    println "<h3>Output:</h3>"
    println openPreTag()
    if (p.exitValue())
    {
        // error on exit
        log("Execution was NOT OK")
        //log(p.err.text)
        println p.err.text
    }
    else
    {
        // no error on exit
        log("Execution finished OK")
        println p.text
    }

    println closePreTag()

    println getPostBody()

    //document.getElementById('hrefObj').style.visibility='hidden' or 'visible'
    //Over.style.display = "inline";
    //Over.style.display = "";

    log("END: Domain Processing Servlet")
}


init()
myresponse()